import axios from './axios'
import qs from 'qs'


export function getDepartmentList() {
    return axios.get('/sys/dept/dept_tree.do').then(res => {
        return Promise.resolve(res.data)
    }, err => {
        return Promise.reject(err)
    })
}

//增加部门 seq =》展示顺序
export function addDepartment({name, parentId, seq, remark}) {
    return axios.post('/sys/dept/add_dept.do', qs.stringify({name, parentId, seq, remark})).then(res => {
        return Promise.resolve(res.data)
    }, err => {
        return Promise.reject(err)
    })
}

//修改部门
export function modDepartment({id, name, parentId, seq, remark}) {
    return axios.post('/sys/dept/update_dept.do', qs.stringify({id, name, parentId, seq, remark})).then(res => {
        return Promise.resolve(res.data)
    }, err => {
        return Promise.reject(err)
    })
}

// delete department
export function deleteDepartment(id) {
    return axios.post('/sys/dept/delete_dept.do', qs.stringify({id})).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}


export function getUserList(deptId) {
    return axios.post('/sys/user/user_list.do', qs.stringify({deptId})).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

export function addUser({loginName, username, telephone, deptId, status, remark, coefficient}) {
    return axios.post("/sys/user/add_user.do", qs.stringify({
        loginName,
        username,
        telephone,
        deptId,
        status,
        remark,
        coefficient
    })).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

export function modUser({id, loginName, username, telephone, deptId, status, remark, coefficient}) {
    return axios.post('/sys/user/update_user.do',
        qs.stringify({
            id,
            loginName,
            username,
            telephone,
            deptId,
            status,
            remark,
            coefficient
        })).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

export function deleteUser(id) {
    return axios.post('/sys/user/delete_user.do', qs.stringify({id})).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}


export function getRoleList() {
    return axios.get('/sys/role/role_list.do').then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

export function addRole({name, type, status, remark}) {
    return axios.post('/sys/role/save_role.do', qs.stringify({
        name,
        type,
        status,
        remark
    })).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

export function modRole({id, name, type, status, remark}) {
    return axios.post('/sys/role/update_role.do', qs.stringify({
        id,
        name,
        type,
        status,
        remark
    })).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

//角色权限树
export function getRoleTree(roleId) {
    return axios.post('/sys/role/role_tree.do', qs.stringify({
        roleId,
        userId: 1
    })).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

export function getRoleUser(roleId) {
    return axios.post('/sys/role/users.do', qs.stringify({roleId})).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

export function changeAcls({roleId, aclIds}) {
    return axios.post('/sys/role/change_acls.do', qs.stringify({
        roleId,
        aclIds
    })).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

export function changeUsers({roleId, userIds}) {
    return axios.post('/sys/role/change_users.do', qs.stringify({
        roleId,
        userIds
    })).then(res => Promise.resolve(res.data), err => Promise.reject(err))
}

/*----------------------------------------------------------------------------------------------------------------------*/

//moduel
export function getModuleTree() {
    return axios.post('/sys/aclModule/acl_module_tree.do').then(res => Promise.resolve(res.data), err => Promise.resolve(err))
}

export function addModuleTree({name, parentId, seq, status, remarks}) {
    return axios.post('/sys/aclModule/save_acl_module.do', qs.stringify({
        name,
        parentId,
        seq,
        status,
        remarks
    })).then(res => Promise.resolve(res.data), err => Promise.resolve(err))
}

export function modModuleTree({id, name, parentId, seq, status, remarks}) {
    return axios.post('/sys/aclModule/update_acl_module.do', qs.stringify({
        id,
        name,
        parentId,
        seq,
        status,
        remarks
    })).then(res => Promise.resolve(res.data), err => Promise.resolve(err))
}

export function deleteModuleTree(aclModuleId) {
    return axios.post('/sys/aclModule/delete_acl_module.do', qs.stringify({aclModuleId})).then(res => Promise.resolve(res.data), err => Promise.resolve(err))
}

/*----------------------------------------------------------------------------------------------------------------------*/

//acl point
export function getAclList(aclModuleId, pageSize = 50) {
    return axios.post('/sys/acl/acl_list.do', qs.stringify({
        aclModuleId,
        pageSize
    })).then(res => Promise.resolve(res.data), err => Promise.resolve(err))
}

export function addAcl({aclModuleId, url, type, seq, status, remark, name}) {
    return axios.post('/sys/acl/save_acl.do', qs.stringify({
        aclModuleId,
        url,
        type,
        seq,
        status,
        remark,
        name
    })).then(res => Promise.resolve(res.data), err => Promise.resolve(err))
}

export function modAcl({id, aclModuleId, url, type, seq, status, remark, name}) {
    return axios.post('/sys/acl/update_acl.do', qs.stringify({
        id,
        aclModuleId,
        url,
        type,
        seq,
        status,
        remark,
        name
    })).then(res => Promise.resolve(res.data), err => Promise.resolve(err))
}

/*----------------------------------------------------------------------------------------------------------------------*/

//获取流程模块下的人员信息
//参数：模块Id
export function getProcessPerson(approveModuleId) {
    return axios.get('/approve/manage/show_approver_by_module.do?approveModuleId=' + approveModuleId).then(res => Promise.resolve(res.data));
}

//新增流程审批流程
//参数：模块ID，初审，复审，终审
export function addProcess(approveModuleId,
                          firstApprover,
                          secondApprover,
                          thirdApprover) {
    return axios.post('/approve/manage/add_approver.do', qs.stringify({
        approveModuleId,
        firstApprover,
        secondApprover,
        thirdApprover
    })).then(res => Promise.resolve(res.data))
}

//修改流程审批流程
//参数：模块ID，初审，复审，终审
export function modProcess(id,
                          approveModuleId,
                          firstApprover,
                          secondApprover,
                          thirdApprover) {
    return axios.post('/approve/manage/update_approver.do', qs.stringify({
        id,
        approveModuleId,
        firstApprover,
        secondApprover,
        thirdApprover
    })).then(res => Promise.resolve(res.data))
}


/*----------------------------------------------------------------------------------------------------------------------*/

/*
* 系统模块
* 新增，修改，列表
* */

/*
* 模块列表接口
* 参数：无
* */
export function getModuleList() {
    return axios.get('/module/manage/show_module_list.do').then(res => Promise.resolve(res.data));
}

/*
* 模块新增接口
* 参数：moduleName
* */
export function addSysModule(moduleName) {
    return axios.post('/module/manage/add_module.do', qs.stringify({moduleName})).then(res => Promise.resolve(res.data));
}

/*
* 修改模块
* 参数id,moduleName
* */
export function modSysModule(id, moduleName) {
    return axios.post('/module/manage/update_module.do', qs.stringify({
        id,
        moduleName
    })).then(res => Promise.resolve(res.data));
}

/*----------------------------------------------------------------------------------------------------------------------*/
